<template>
  <div class="crud-container">
    <!-- 顶部操作栏 -->
    <div class="vs-search-box">
      <el-form ref="form" :inline="true" :model="paging" label-width="110px">
        <el-form-item label="分类名称">
          <el-input clearable style="width:200px" v-model="paging.title"></el-input>
          <el-button style="margin-left: 10px" @click="fetchData()" type="primary">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="operation-bar">
      <el-button type="primary" @click="handleAdd">新增</el-button>
    </div>

    <!-- 数据表格 -->
    <el-table :data="tableData" border style="width: 100%" v-loading="loading">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="title" label="标题"></el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="vs-paging-box">
      <!-- <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="paging.page"
        :page-size="paging.limit"
        layout="prev, pager, next, jumper"
        :total="paging.total"
      ></el-pagination> -->
    </div>

    <!-- 新增/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="50%">
      <el-form :model="form" :rules="rules" ref="form" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input style="width: 80%" v-model="form.title"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

export default {
  data() {
    return {
      tableData: [],
      loading: false,
      dialogVisible: false,
      dialogTitle: "新增",
      form: {
        title: ""
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }]
      },
      currentIndex: -1,
      paging: {
        page: 1,
        limit: 10,
        total: 0, //总数
        title: ""
      }
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    // 获取数据
    async fetchData() {
      this.loading = true;
      let { code, data } = await this.$Http.getGoodsTmpCate({
        // page: this.paging.page,
        // limit: this.paging.limit,
        title: this.paging.title
      });
      if (code) {
        this.tableData = data;
        // this.paging.total = data.count;
      }
      this.loading = false;
    },
    handleCurrentChange(val) {
      this.paging.page = val;
      this.fetchData();
    },

    // 新增
    handleAdd() {
      this.dialogTitle = "新增";
      this.form = {
        id: null,
        title: ""
      };
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },

    // 编辑
    handleEdit(index, row) {
      this.dialogTitle = "编辑";
      this.form = {
        id: row.id,
        title: row.title
      };
      this.currentIndex = index;
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },

    // 删除
    handleDelete(index, row) {
      this.$confirm("确认删除该记录吗?", "提示", {
        type: "warning"
      })
        .then(() => {
          this.loading = true;
          this.$Http.delGoodsTmpCate({id: row.id})
            .then(() => {
              this.$message.success("删除成功");
              this.fetchData();
            })
            .catch(() => {
              this.loading = false;
            });
        })
        .catch(() => {});
    },

    // 提交表单
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true;
          const params = this.form.id ? {
            id: this.form.id,
            title: this.form.title
          }:{
            title: this.form.title
          }
          const promise = this.$Http.setGoodsTmpCate(params)
          promise
            .then(() => {
              this.$message.success(this.form.id ? "修改成功" : "新增成功");
              this.dialogVisible = false;
              this.fetchData();
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    }
  }
};
</script>

<style scoped>
.crud-container {
  padding: 20px;

  background-color: #ffffff;
  border-radius: 10px;
}

.operation-bar {
  margin-bottom: 20px;
  text-align: right;
}

.el-table {
  margin-top: 20px;
}
</style>